package com.sos.flex.data.adapters
{
	import com.sos.core.Position;
	import com.sos.core.cards.ActionCard;
	import com.sos.core.cards.Deck;
	import com.sos.core.cards.GoalkeeperCard;
	import com.sos.core.cards.PlayerCard;
	import com.sos.flex.data.FlexCollection;
	import com.sos.flex.data.FlexDeck;

	public class DeckAdapter
	{

		public static function updateFlexDeck(source:Deck, destination:FlexDeck):void
		{
			// Clears the deck
			destination.clearFormation();
			destination.clearActionCards();
			// Copy action cards
			for (var i:int = 0; i < source.actionCards.length; i++)
			{
				destination.addActionCard( destination.collection.getCardByInstanceId(source.actionCards[i].instanceId) as ActionCard );
			}
			// Copy formation
			if (source.hasGoalkeeper)			destination.setPlayerCardByPosition(Position.GOALKEEPER, 	destination.collection.getCardByInstanceId( source.goalkeeper.instanceId) as GoalkeeperCard);
			if (source.hasDefender01)			destination.setPlayerCardByPosition(Position.DEFENDER_01, 	destination.collection.getCardByInstanceId( source.defender01.instanceId) as PlayerCard);
			if (source.hasDefender02)			destination.setPlayerCardByPosition(Position.DEFENDER_02, 	destination.collection.getCardByInstanceId( source.defender02.instanceId) as PlayerCard);
			if (source.hasMidfield)				destination.setPlayerCardByPosition(Position.MIDFIELD, 		destination.collection.getCardByInstanceId( source.midfield.instanceId) as PlayerCard);
			if (source.hasForward01)			destination.setPlayerCardByPosition(Position.FORWARD_01, 	destination.collection.getCardByInstanceId( source.forward01.instanceId) as PlayerCard);
			if (source.hasForward02)			destination.setPlayerCardByPosition(Position.FORWARD_02, 	destination.collection.getCardByInstanceId( source.forward02.instanceId) as PlayerCard);
			if (source.hasSubstitutionDefence)	destination.setPlayerCardByPosition(Position.SUB_DEFENCE, 	destination.collection.getCardByInstanceId( source.substitutionDefence.instanceId) as PlayerCard);
			if (source.hasSubstitutionMidfield)	destination.setPlayerCardByPosition(Position.SUB_MIDFIELD, 	destination.collection.getCardByInstanceId( source.substitutionMidfield.instanceId) as PlayerCard);
			if (source.hasSubstitutionForward)	destination.setPlayerCardByPosition(Position.SUB_FORWARD, 	destination.collection.getCardByInstanceId( source.substitutionForward.instanceId) as PlayerCard);
			// Updates all left cards
			destination.updateLeftCards();
			// 'Loaded' flag forcing
			if (source.loaded) destination.forceLoaded();
		}
		
		public static function toFlexDeck(deck:Deck, collection:FlexCollection):FlexDeck
		{
			var flexDeck:FlexDeck = new FlexDeck(deck.id, deck.name, collection); 
			updateFlexDeck(deck, flexDeck);
			return flexDeck;
		}
		
		
	}
}